﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FT.TouchScreen.Terminal.Model;
using System.IO;

///
namespace FT.TouchScreen.Terminal.Controls
{
   /// <summary>
   /// 多图片展示工具
   /// </summary>
    public partial class MultipleImageShowPanel : UserControl
    {
        private List<IImageAdEntity> imageAdList = null;

        /// <summary>
        /// 图片广告对象
        /// </summary>
        public List<IImageAdEntity> ImageAdList
        {
            get { return imageAdList; }
            set { imageAdList = value; }
        }

       

        /// <summary>
        /// 默认切换时间
        /// </summary>
        public int ChangeImageSecond
        {
            get { return this.timer1.Interval/1000; }
            set { 

            this.timer1.Interval = value * 1000;
            }
        }

        public MultipleImageShowPanel()
        {
            InitializeComponent();
        }

        private void MultipleImageShowPanel_Load(object sender, EventArgs e)
        {
           // FT.Commons.Tools.WinFormHelper.CenterVer(this.picLeft);
           // FT.Commons.Tools.WinFormHelper.CenterVer(this.picRight);
            if (!this.DesignMode)
            {
                try
                {
                    this.picLeft.Location = new Point(5, (this.Height - this.picRight.Height) / 2);
                    this.picRight.Location = new Point(this.Width - this.picRight.Width - 5, (this.Height - this.picRight.Height) / 2);
                    //this.timer1.Start();
                }
                catch
                {
                }
            }
        }

        private int mouseDownX = -1;
        private int mouseDownY = -1;

        /// <summary>
        /// 当前图片显示索引
        /// </summary>
        private int currentAdIndex = 0;

        /// <summary>
        /// 鼠标滑动范围
        /// </summary>
        private  const int mouseMoveDistance = 20;

        private void MultipleImageShowPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
                mouseDownX = e.X;
                mouseDownY = e.Y;
            }
        }

        private void SetAdImage(int index)
        {
           
            currentAdIndex = index;
            this.BackgroundImage = this.imageAdList[currentAdIndex].AdImage;
            if (this.imageAdList.Count <= 1)
            {
                if (this.NeedShowArraw)
                    this.picRight.Visible = this.picLeft.Visible = false;
            }
            else
            {
                if (currentAdIndex == 0)
                {
                    if (this.NeedShowArraw)
                    {
                        this.picRight.Visible = true;
                        this.picLeft.Visible = false;
                    }

                }
                else if (currentAdIndex == this.imageAdList.Count - 1)
                {
                    if (this.NeedShowArraw)
                    {
                        this.picRight.Visible = false;
                        this.picLeft.Visible = true;
                    }

                }
                   
                else
                {
                    if (this.NeedShowArraw)
                    {
                        this.picRight.Visible = true;
                        this.picLeft.Visible = true;
                    }
                }
            }
        }
        /// <summary>
        /// 显示上张图片广告
        /// </summary>
        private void ShowPreAd()
        {
            if (this.imageAdList.Count > 1)
            {
                if (currentAdIndex >0)
                {
                    currentAdIndex--;
                }
                else
                {
                    //currentAdIndex = this.imageAdList.Count-1;
                }

                this.SetAdImage(currentAdIndex);
            }
            else
            {
                if (this.NeedShowArraw)
                {
                    this.picLeft.Visible = this.picRight.Visible = false;
                }
            }
        }

        private Boolean needShowArraw = true;

        public Boolean NeedShowArraw
        {
            get { return needShowArraw; }
            set { needShowArraw = value;
            if (value == false)
                this.picRight.Visible = this.picLeft.Visible = false;
            }
        }

        

        public void InitPicFolder(string dir)
        {
            if (this.imageAdList == null)
            {
                this.imageAdList = new List<IImageAdEntity>();
            }
            DirectoryInfo dirInfo=new DirectoryInfo(dir);
            if(dirInfo.Exists)
            {
                
                this.imageAdList.Clear();
                FileInfo[] files = dirInfo.GetFiles();
                IImageAdEntity imageAdEntity = null;
                for (int i = 0; i < files.Length; i++)
                {
                    imageAdEntity = new SimpleImageAdEntity();
                    imageAdEntity.AdImage = Image.FromFile(files[i].FullName);
                    this.imageAdList.Add(imageAdEntity);
                }
                this.currentAdIndex = 0;
                this.SetAdImage(this.currentAdIndex);
                this.timer1.Stop();
                this.timer1.Start();
            }
        }

        /// <summary>
        /// 显示下张图片广告
        /// </summary>
        private void ShowNextAd()
        {
            if (this.imageAdList.Count > 1)
            {
                if (currentAdIndex < this.imageAdList.Count - 1)
                {
                    currentAdIndex++;
                   
                }
                else
                {
                    //currentAdIndex = 0;
                   
                }
                this.SetAdImage(currentAdIndex);
            }
            else
            {
                if(this.NeedShowArraw)
                     this.picLeft.Visible = this.picRight.Visible = false;
            }
        }

        private void MultipleImageShowPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
            {
                
            }
        }

        private void MultipleImageShowPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
                if (mouseDownX > 0)
                {
#if DEBUG
                    Console.WriteLine("鼠标单击位置X为：" + mouseDownX.ToString() + "；Y为：" + mouseDownY.ToString()
                        + "鼠标移动后松开的X为：" + e.X.ToString()
                        + "；Y为：" + e.Y.ToString()

                        + "移动距离为：" + Math.Abs((e.X - mouseDownX)).ToString());
#endif
                    if (Math.Abs((e.X - mouseDownX)) > mouseMoveDistance)
                    {
                        if (e.X < mouseDownX)
                        {
                            ShowNextAd();
                        }
                        else
                        {
                            ShowPreAd();
                        }
                    }
                }
                mouseDownX = mouseDownY = -1;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.timer1.Stop();
            if (this.imageAdList.Count > 1)
            {
                this.ShowNextAd();
                this.timer1.Start();
            }
        }

        private void picLeft_Click(object sender, EventArgs e)
        {
            this.ShowPreAd();
        }

        private void picRight_Click(object sender, EventArgs e)
        {
            this.ShowNextAd();
        }
    }
}
